package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.PrimaryKey;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.YdTeamT;
import com.jshoperxms.entity.YdTeamTargetMemberT;
import com.jshoperxms.entity.YdTeamTargetRecordRdT;
import com.jshoperxms.service.MemberTService;
import com.jshoperxms.service.YdTeamTargetMemberTService;
import com.jshoperxms.service.YdTeamTargetRecordRdTService;
import com.jshoperxms.service.YdTeamTargetTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 2019/4/8.
 * Des:
 */
@RestController
@RequestMapping("/api/teamtarget/record")
public class YdTeamTargetRecordRdTContoller extends BaseTController {


    @Resource
    private MemberTService memberTService;

    @Resource
    private YdTeamTargetTService ydTeamTargetTService;

    @Resource
    private YdTeamTargetMemberTService ydTeamTargetMemberTService;

    @Resource
    private YdTeamTargetRecordRdTService ydTeamTargetRecordRdTService;

    /**
     * 保存目标每日打卡朗读或者图文记录记录
     * @param rq
     * @return
     */
    @RequestMapping(value="/saveDailyTargetRecord",method= RequestMethod.POST)
    public ResponseEntity saveDailyTargetRecord(@RequestBody YdTeamTargetRecordRdT rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())&&StringUtils.isNotBlank(rq.getDotargetdate())){
            //查询目标下是否当天已经有打卡数据,如果没有当前目标的当前日期的打卡天数，则打卡天数+1
            Criterion criterion1=Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq("dotargetdate",rq.getDotargetdate())).add(Restrictions.eq("targetid",rq.getTargetid()));
            YdTeamTargetRecordRdT ydTeamTargetRecordRdT=this.ydTeamTargetRecordRdTService.findOneByCriteria(YdTeamTargetRecordRdT.class,criterion1);
            if(ydTeamTargetRecordRdT==null) {
                Criterion criterion2=Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq("targetid",rq.getTargetid()));
                YdTeamTargetMemberT ydTeamTargetMemberT=this.ydTeamTargetMemberTService.findOneByCriteria(YdTeamTargetMemberT.class,criterion2);
                if(ydTeamTargetMemberT!=null){
                    ydTeamTargetMemberT.setCurrenttimes(ydTeamTargetMemberT.getCurrenttimes()+1);
                    ydTeamTargetMemberT.setUpdatetime(DateTools.getSystemTimeLong());
                    ydTeamTargetMemberT.setVersiont(ydTeamTargetMemberT.getVersiont()+1);
                    this.ydTeamTargetMemberTService.update(ydTeamTargetMemberT);
                }
            }
            Criterion criterion= Restrictions.eq("wxopenid",rq.getWxopenid());
            MemberT memberT=this.memberTService.findOneByCriteria(MemberT.class,criterion);
            if(memberT!=null) {
                YdTeamTargetRecordRdT bean = new YdTeamTargetRecordRdT();
                bean.setId(PrimaryKey.get().toString());
                bean.setFileurl(rq.getFileurl());
                bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                bean.setCreatetime(DateTools.getSystemTimeLong());
                bean.setCreatorid("0");
                bean.setUpdatetime(DateTools.getSystemTimeLong());
                bean.setVersiont(1);
                bean.setFiletype(rq.getFiletype());
                bean.setDuration(rq.getDuration());
                bean.setGbiaoyang(0);
                bean.setGleafs(20);
                bean.setGscore(90);
                bean.setGruntimecount(1);
                bean.setGsharecount(0);
                bean.setFileimgs(rq.getFileimgs());
                bean.setArticlecontent(rq.getArticlecontent());
                bean.setAuthornick(rq.getAuthornick());
                bean.setAuthorheadimg(rq.getAuthorheadimg());
                bean.setWxopenid(rq.getWxopenid());
                bean.setCurrentpublishcount(rq.getCurrentpublishcount());
                bean.setTitle(rq.getTitle());
                bean.setIspublic("0");//默认不公开到朗读乐园
                bean.setGcomment(0);
                bean.setTeamcategory(StaticKey.EMPTY);
                bean.setTeamcategoryid(StaticKey.EMPTY);
                bean.setRecommend("0");//不推荐
                bean.setDraft("0");
                bean.setTeamid(StaticKey.EMPTY);
                bean.setGfollowreadingcount(0);
                bean.setMemberid(memberT.getId());
                bean.setTargettype(rq.getTargettype());
                bean.setDotargetyear(rq.getDotargetyear());
                bean.setDotargetmonth(rq.getDotargetmonth());
                bean.setDotargetday(rq.getDotargetday());
                bean.setDotargetdate(rq.getDotargetdate());
                bean.setTargetid(rq.getTargetid());
                this.ydTeamTargetRecordRdTService.save(bean);
                //rq.setId(bean.getId());//把目标朗读记录的id赋值给切面aspect需要的地方
                responsedata.put(TagWords.SUCCESS, true);
                responsedata.put("targetrecordid", bean.getId());
            }else{
                responsedata.put(TagWords.SUCCESS,false);
                responsedata.put(TagWords.MSG, WarnWrods.EMPTY_RECORD);
            }

        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);

        }
        return Response.success(responsedata);

    }


    /**
     * 根据当前日期查询是否有打卡记录
     * @param rq
     * @return
     */
    @RequestMapping(value="/findTargetRecordByDoDate",method= RequestMethod.POST)
    public ResponseEntity findTargetRecordByDoDate(@RequestBody YdTeamTargetRecordRdT rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())&&StringUtils.isNotBlank(rq.getDotargetdate())) {
            //查询目标下是否当天已经有打卡数据,如果没有当前目标的当前日期的打卡天数，则打卡天数+1
            Criterion criterion1 = Restrictions.and(Restrictions.eq("wxopenid", rq.getWxopenid())).add(Restrictions.eq("dotargetdate", rq.getDotargetdate())).add(Restrictions.eq("targetid", rq.getTargetid()));
            YdTeamTargetRecordRdT ydTeamTargetRecordRdT = this.ydTeamTargetRecordRdTService.findOneByCriteria(YdTeamTargetRecordRdT.class, criterion1);
            if (ydTeamTargetRecordRdT != null) {

                responsedata.put("ispthec","已经打卡");
            }else{
                responsedata.put("ispthec","未打卡");
            }
            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }


    /**
     * 查询目标内容记录
     * @param rq
     * @return
     */
    @RequestMapping(value="/findTargetRecords",method= RequestMethod.POST)
    public ResponseEntity findTargetRecords(@RequestBody YdTeamTargetRecordRdT rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getTargetid())){
            Criterion criterion=Restrictions.and(Restrictions.eq("targetid",rq.getTargetid())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
            List<YdTeamTargetRecordRdT> list=this.ydTeamTargetRecordRdTService.findByCriteriaByPage(YdTeamTargetRecordRdT.class,criterion, Order.desc(TagWords.CREATETIME),1,100);
            responsedata.put("results",list);
            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG,WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }


    /**
     * 获取目标朗读记录详情
     * @param rq
     * @return
     */
    @RequestMapping(value="/findTargetRecorddetail",method= RequestMethod.POST)
    public ResponseEntity findTargetRecorddetail(@RequestBody YdTeamTargetRecordRdT rq) {
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getId())) {
            YdTeamTargetRecordRdT bean=this.ydTeamTargetRecordRdTService.findByPK(YdTeamTargetRecordRdT.class,rq.getId());
            if(bean!=null){
                responsedata.put("publishtime",DateTools.formateLongDateToString(bean.getCreatetime()));
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.BEAN,bean);
            }
        } else {
            responsedata.put(TagWords.SUCCESS, false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);

        }
        return Response.success(responsedata);

    }
}
